<!--
 * @Author: your name
 * @Date: 2021-06-27 15:26:38
 * @LastEditTime: 2021-06-28 17:07:56
 * @LastEditors: Please set LastEditors
 * @Description: 权限管理
 * @FilePath: \briup\练习\briup-ui\src\pages\admin\sys\privilege\List.vue
-->
<template>
  <div>
    <div>
      <el-button type="primary" @click="toAdd">添加</el-button>
    </div>
    <!-- 表格 -->
    <div
      v-loading="loading"
      class="table"
      style="margin-top: 10px"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table :data="tableData" style="width: 100%" row-key="id">
        <el-table-column prop="name" label="名称" align="center" width="100" />
        <el-table-column prop="route" label="路径" align="center" width="500" />
        <el-table-column prop="type" label="类型" align="center" />
        <el-table-column prop="num" label="序号" align="center" />
        <el-table-column prop="icon" label="图标" align="center" />
        <el-table-column prop="hidden" label="状态" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.hidden === 0" type="success">显示</el-tag>
            <el-tag v-else type="info">隐藏</el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              class="red_info"
              type="text"
              size="small"
              @click="toDelete(scope.row.id)"
            >删除</el-button>
            <el-button
              class="green_info"
              type="text"
              size="small"
              @click="toEdit(scope.row)"
            >编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="visible" @close="toclose">
      <el-form ref="ruleForm" :model="form" size="medium" :rules="rules">
        <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="路径" :label-width="formLabelWidth" prop="route">
          <el-input v-model="form.route" autocomplete="off" />
        </el-form-item>
        <el-form-item label="类型" :label-width="formLabelWidth" prop="type">
          <el-select v-model="form.type" placeholder="请选择" clearable>
            <el-option label="菜单" value="menu" />
            <el-option label="方法" value="method" />
          </el-select>
          <el-popover
            placement="top-start"
            title="提示"
            width="300"
            trigger="hover"
            content="菜单控制着用户登录后可以动态显示的菜单树；方法为具体的权限，控制着是否可以调用某些接口"
          >
            <el-button slot="reference" type="text">
              <i class="el-icon-location" />
            </el-button>
          </el-popover>
        </el-form-item>
        <el-form-item label="父权限" :label-width="formLabelWidth">
          <el-cascader
            v-model="form.parentId"
            :options="tableData"
            :props="{
              expandTrigger: 'hover',
              value: 'id',
              label: 'name',
              checkStrictly: true,
            }"
            clearable
          />
        </el-form-item>
        <el-form-item label="序号" :label-width="formLabelWidth">
          <el-input v-model="form.num" autocomplete="off" />
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.hidden"
            active-text="隐藏"
            inactive-text="显示"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth">
          <el-input
            v-model="form.description"
            autocomplete="off"
            type="textarea"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">取 消</el-button>
        <el-button type="primary" size="small" @click="toSave">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { findPrivilegeTree, saveOrUpdate, deleteById } from '@/api/privilege'
export default {
  data() {
    return {
      tableData: [],
      loading: false,
      visible: false,
      title: '',
      formLabelWidth: '80px',
      form: {},
      rules: {
        name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
        route: [{ required: true, message: '请输入权限路径', trigger: 'blur' }],
        type: [
          { required: true, message: '请输入权限类型', trigger: 'change' }
        ]
      }
    }
  },
  computed: {},
  created() {
    this.querytableData()
  },
  mounted() {},
  methods: {
    /**
     * @description: 通过查询树查询数据
     * @param {*}
     * @return {*}
     */
    async querytableData() {
      this.loading = true
      const res = await findPrivilegeTree()
      this.foo(res.data)
      this.tableData = res.data
      this.loading = false
    },
    /**
     * @description: 处理权限的菜单级别
     * @param {*} tableData
     * @return {*}
     */
    foo(tableData) {
      for (const t of tableData) {
        if (t.children && t.children.length === 0) {
          delete t.children
        } else {
          this.foo(t.children)
        }
      }
    },
    /**
     * @description: 添加按钮事件
     * @param {*}
     * @return {*}
     */
    toAdd() {
      this.visible = true
      this.form = {}
      this.title = '添加权限'
    },
    /** 保存数据
     * @description:
     * @param {*}
     * @return {*}
     */
    toSave() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          const res = await saveOrUpdate(this.form)
          this.$message.success({ message: res.message })
          this.toclose()
          this.querytableData()
        } else {
          return false
        }
      })
    },
    /** 清空模态框
     * @description:
     * @param {*}
     * @return {*}
     */
    toclose() {
      this.$refs.ruleForm.resetFields()
      setTimeout(() => {
        this.visible = false
      }, 100)
    },
    /** 编辑按钮事件
     * @description:
     * @param {*} row
     * @return {*}
     */
    toEdit(row) {
      this.title = '编辑权限'
      this.form = { ...row }
      this.visible = true
    },
    /** 删除按钮事件
     * @description:
     * @param {*} id
     * @return {*}
     */
    toDelete(id) {
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await deleteById({ id })
          this.$message({ type: 'success', message: res.message })
          this.querytableData()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>
<style scoped>
</style>
